﻿using XDQ.VAT.Localization;
using Volo.Abp.AuditLogging;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.FeatureManagement;
using Volo.Abp.Identity;
using Volo.Abp.IdentityServer;
using Volo.Abp.Localization;
using Volo.Abp.Localization.ExceptionHandling;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
using Volo.Abp.Validation.Localization;
using Volo.Abp.VirtualFileSystem;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System;
using XDQ.VAT.Options;
using Volo.Abp.FeatureManagement.Localization;

namespace XDQ.VAT
{
    [DependsOn(
        typeof(AbpAuditLoggingDomainSharedModule),
        typeof(AbpBackgroundJobsDomainSharedModule),
        typeof(AbpFeatureManagementDomainSharedModule),
        typeof(AbpIdentityDomainSharedModule),
        typeof(AbpIdentityServerDomainSharedModule),
        typeof(AbpPermissionManagementDomainSharedModule),
        typeof(AbpSettingManagementDomainSharedModule),
        typeof(AbpTenantManagementDomainSharedModule)
        )]
    public class VATDomainSharedModule : AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            VATGlobalFeatureConfigurator.Configure();
            VATModuleExtensionConfigurator.Configure();

            PreConfigure<GlobalizerOptions>(options =>
            {
                options.DefaultLanguage = "zh-hans";
            });

        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                options.FileSets.AddEmbedded<VATDomainSharedModule>();
            });

            Configure<AbpLocalizationOptions>(options =>
            {
                options.Resources
                    .Add<VATResource>("zh-hans")
                    .AddBaseTypes(typeof(AbpValidationResource))
                    .AddBaseTypes(typeof(AbpFeatureManagementResource))
                    .AddVirtualJson("/Localization/VAT");

                options.DefaultResourceType = typeof(VATResource);
            });
            //Configure<AbpFe>

            Configure<AbpExceptionLocalizationOptions>(options =>
            {
                options.MapCodeNamespace("Volo.Abp.Authorization", typeof(VATResource));
                options.MapCodeNamespace("Volo.Abp.FeatureManagement", typeof(VATResource));
                options.MapCodeNamespace("Volo.Abp", typeof(VATResource));
                options.MapCodeNamespace("Volo", typeof(VATResource));
                options.MapCodeNamespace("VAT", typeof(VATResource));
            });
            var config = context.Services.GetRequiredServiceLazy<IConfiguration>();
            Configure<GlobalizerOptions>(options =>
            {
                if (config.IsValueCreated)
                {
                    string lang = config.Value.GetValue<string>("Globalizer:DefaultLanguage");
                    if (!string.IsNullOrWhiteSpace(lang))
                    {
                        options.DefaultLanguage = lang;
                    }
                }
                else
                {
                    Console.WriteLine("Ohho~No IConfiguration!");
                }
            });

        }
    }
}
